Option Explicit On
Imports System.Text.RegularExpressions
Imports system
Imports System.IO
Imports System.IO.StreamWriter
Module ModuleEDIVDA4915
    Public VD4915Message As Array
    Dim lista As ArrayList
    Structure MyStructure
        Public Data As Date
        Public Peca As String
        Public quantidade As Integer
    End Structure
    Public Fitem As MyStructure
    Public VD4915Lines As Integer
    Public t As Integer

    Public Sub LoadTextFile()
        Dim sourceFile As String = M_551.dir_all
        If File.Exists(sourceFile) Then
            VD4915Message = File.ReadAllLines(sourceFile)
            VD4915Lines = VD4915Message.GetUpperBound(t)
        End If
        ciclo4915()
    End Sub
    Private Sub Pc551(ByVal str551 As String)
        Dim versao As String
        versao = str551.Substring(3, 2).Trim
        M_551.cliente = str551.Substring(5, 9).Trim
        M_551.fornecedor = str551.Substring(14, 9).Trim
        M_551.nr_transmit_antigo = CLng(str551.Substring(23, 5).Trim)
        M_551.nr_transmit_novo = CLng(str551.Substring(28, 5).Trim)
        M_551.data_transmit = "20" & (str551.Substring(33, 6)).Trim
        M_551.data_inicio = "20" & (str551.Substring(39, 6)).Trim
        M_551.i = 10000
        '************************************************** Create File 4915 *********************************
        M_551.X = My.Settings.Destino & M_551.supplier & "4915" & Mid(My.Forms.Form1.TextBox2.Text, Len(M_551.supplier) + 6) & ".csv"
        If File.Exists(M_551.X) Then
            MsgBox("File Already exists in the directory.", MsgBoxStyle.OkOnly)
            End
        Else
            Dim fs As New FileStream(M_551.X, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
            fs.Close()
        End If
        '************************************************** End *********************************
    End Sub
    Private Sub Pc552(ByVal str552 As String)
        Dim versao, cod_fabrica_cliente, data_REL As String
        Dim cod_artigo_fornecedor, local_recepcao As String
        Dim localizacao_armazem_cliente As String
        Dim pn_critico As String
        Dim indicador_requerimento As String 'S - production; E - services
        Dim horizonte_DCI As String
        Dim indicador_envio As String ' blank - reception date ; V - shipping date
        Dim finis_code As String
        Dim ordem_entrega As String
        Dim localizacao_producao As String
        versao = str552.Substring(3, 2).Trim
        cod_fabrica_cliente = str552.Substring(5, 3).Trim
        M_551.nr_rel = CLng(str552.Substring(8, 9).Trim)
        data_REL = (str552.Substring(17, 6)).Trim
        M_551.DataInicio = ""
        data_REL = "20" & data_REL
        Fitem.Peca = str552.Substring(23, 22).Trim
        M_551.PN = Fitem.Peca
        M_551.PN2 = "?"
        cod_artigo_fornecedor = str552.Substring(45, 22).Trim
        local_recepcao = str552.Substring(67, 5).Trim
        localizacao_armazem_cliente = str552.Substring(72, 7).Trim
        pn_critico = str552.Substring(79, 1).Trim
        indicador_requerimento = str552.Substring(80, 1).Trim
        Select Case indicador_requerimento
            Case "S"
                M_551.indicador_req = "Production"
            Case "E"
                M_551.indicador_req = "Services"
            Case Else
                M_551.indicador_req = ""
        End Select
        horizonte_DCI = (str552.Substring(81, 6)).Trim
        If CDbl(Mid(horizonte_DCI, 3, 1)) = 0 Then
            M_551.DataFim = Mid(horizonte_DCI, 4, 1)
        Else
            M_551.DataFim = Mid(horizonte_DCI, 4, 2)
        End If
        horizonte_DCI = "20" & horizonte_DCI
        Select Case str552.Substring(87, 1).Trim
            Case ""
                indicador_envio = "Receiving dates"
            Case "V"
                indicador_envio = "Shipping dates"
            Case Else
                indicador_envio = ""
        End Select
        finis_code = str552.Substring(88, 12).Trim
        M_551.AREADEENT = Left(local_recepcao, 2)
        M_551.FINIS = finis_code
        ordem_entrega = str552.Substring(100, 10).Trim
        localizacao_producao = str552.Substring(110, 14).Trim
    End Sub

    Private Sub pc553(ByVal str553 As String)
        Dim quantidade_acumulada As Long
        Dim versao, data_recep As String
        Dim ASN_nr As String
        Dim ASN_data As String
        Dim quant As Long
        Dim status As String ' I - received
        Dim i As Integer
        versao = str553.Substring(3, 2).Trim
        quantidade_acumulada = CLng(str553.Substring(5, 10).Trim)
        For i = 15 To (str553.Length - 15) Step 33
            If str553.Substring((i + 33), 1).Trim = "" Then
                Exit Sub
            End If
            If CDbl(str553.Substring((i), 32).Trim) = 0 Then
                Exit Sub
            End If
            data_recep = "20" & (str553.Substring(i, 6))
            ASN_nr = str553.Substring((i + 6), 8).Trim
            ASN_data = "20" & (str553.Substring((i + 14), 6)).Trim
            quant = CLng(str553.Substring((i + 20), 12))
            status = str553.Substring((i + 32), 1).Trim
        Next i
    End Sub
    Private Sub pc554(ByVal str554 As String)
        Dim versao, dci_data, dci_hora As String
        Dim dci_quant As Long
        Dim dci_indicador As String
        Dim i As Integer
        Dim j As Integer = M_554.j
        versao = str554.Substring(3, 2).Trim
        For i = 5 To (str554.Length - 15) Step 20
            If M_554.j = 12 Then j = 0
            j = j + 1
            dci_data = "20" & (str554.Substring(i, 6))
            If dci_data = "20000000" Then
                dci_data = "00000000"
            End If
            If str554.Substring((i + 6), 4).Trim = "" Then
                dci_hora = "0000"
            Else
                dci_hora = (str554.Substring((i + 6), 4)).Trim
            End If
            dci_quant = CLng(str554.Substring((i + 10), 9).Trim)
            dci_indicador = str554.Substring((i + 19), 1).Trim
            M_554.data(j) = dci_data
            M_554.quant(j) = dci_quant
        Next i
        M_554.j = j
        If Len(M_551.supplier) > 2 And M_554.j > 6 Then
            save1()
            M_554.j = 0
        ElseIf Len(M_551.supplier) > 2 And M_554.j <= 6 Then
        Else
            save()
            M_554.j = 0
        End If
    End Sub
    Private Sub pc555(ByVal str555 As String)
        Dim versao, local_consumo_stock As String
        Dim ultima_quant_acumulada As Long
        versao = str555.Substring(3, 2).Trim
        local_consumo_stock = str555.Substring(22, 14).Trim
        ultima_quant_acumulada = CLng(str555.Substring(36, 10))
    End Sub
    Private Sub pc556(ByVal str556 As String)
        Dim versao, pack_cod_cliente, pack_cod_fornecedor As String
        Dim capacidade_pack_type As String
        versao = str556.Substring(3, 2).Trim
        pack_cod_cliente = str556.Substring(5, 22).Trim
        pack_cod_fornecedor = str556.Substring(27, 22).Trim
        capacidade_pack_type = str556.Substring(49, 7)
    End Sub
    Private Sub pc557(ByVal str557 As String)
        Dim versao, dci_text1, dci_text2, dci_text3, text1, text3 As String
        versao = str557.Substring(3, 2).Trim
        dci_text1 = str557.Substring(5, 27).Trim
        text1 = (str557.Substring(33, 4) & " " & str557.Substring(42, 1)).Trim
        dci_text2 = str557.Substring(45, 40).Trim
        dci_text3 = str557.Substring(85, 40).Trim
        text3 = str557.Substring(105, 20).Trim
    End Sub
    Private Sub Pc559(ByVal str559 As String)
        Dim contador551, contador552, contador553, contador554, contador556, contador557, contador559, contador555 As String
        Dim versao As String
        versao = str559.Substring(3, 2).Trim
        contador551 = CStr(str559.Substring(5, 7))  ' nr of type 551
        contador552 = CStr(str559.Substring(12, 7)) ' nr of type 552
        contador553 = CStr(str559.Substring(19, 7)) ' nr of type 553
        contador554 = CStr(str559.Substring(26, 7)) ' nr of type 554
        contador556 = CStr(str559.Substring(33, 7)) ' nr of type 556
        contador557 = CStr(str559.Substring(40, 7)) ' nr of type 557
        contador559 = CStr(str559.Substring(47, 7)) ' nr of type 559
        contador555 = CStr(str559.Substring(54, 7)) ' nr of type 555
    End Sub
    Private Sub ciclo4915()
        For t = 0 To VD4915Lines
            Select Case Left(CStr(VD4915Message.GetValue(t)), 3)
                Case CStr(551)
                    Pc551(CStr(VD4915Message.GetValue(t)))
                Case CStr(552)
                    Pc552(CStr(VD4915Message.GetValue(t)))
                Case CStr(553)
                    pc553(CStr(VD4915Message.GetValue(t))) ' request reconciliation
                Case CStr(554)
                    pc554(CStr(VD4915Message.GetValue(t))) ' new requests
                Case CStr(555)
                    pc555(CStr(VD4915Message.GetValue(t))) ' adicional information
                Case CStr(556)
                    pc556(CStr(VD4915Message.GetValue(t))) ' Code Package 
                Case CStr(557)
                    pc557(CStr(VD4915Message.GetValue(t))) ' Text transfer information (if exists)
                Case CStr(559)
                    Pc559(CStr(VD4915Message.GetValue(t))) ' delivery date for trailer
            End Select
        Next
    End Sub
    Private Sub save()
        Dim x As String = ""
        Dim Y As String = ""
        Dim j As Integer = 1
        Dim i As Integer = 1
        For j = 1 To 6
            x = x & ";" & Mid(M_554.data(j), 1, 4) & "/" & Mid(M_554.data(j), 5, 2) & "/" & Mid(M_554.data(j), 7, 2)
        Next
        For i = 1 To 6
            Y = Y & ";" & M_554.quant(i)
        Next

        '*********************** Save ****************************
        M_551.i = M_551.i + 1
        Select Case M_551.i
            Case Is = 10001
                Dim newline As IO.StreamWriter
                newline = New IO.StreamWriter(M_551.X, True)
                newline.WriteLine(M_551.supplier & " DCI" & ";" & "" & ";" & "Client:" & ";" & M_551.cliente & ";" & "Supplier:" & ";" & M_551.fornecedor)
                newline.WriteLine("" & ";" & "" & ";" & "Old Transmission:" & ";" & M_551.nr_transmit_antigo & ";" & "New Transmission:" & ";" & M_551.nr_transmit_novo & ";" & "Requirement Indicator")
                newline.WriteLine("" & ";" & "" & ";" & "Nr" & ";" & M_551.nr_rel & ";" & "Date:" & ";" & Mid(M_551.data_transmit, 1, 4) & "/" & Mid(M_551.data_transmit, 5, 2) & "/" & Mid(M_551.data_transmit, 7, 2) & ";" & M_551.indicador_req)
                newline.WriteLine()
                newline.WriteLine("PN" & x)
                newline.WriteLine(M_551.PN & Y)
                newline.Close()
            Case Is > 10001
                Dim newline As IO.StreamWriter
                newline = New IO.StreamWriter(M_551.X, True)
                newline.WriteLine(M_551.PN & Y)
                newline.Close()
        End Select
        '*************************** End ********************************
    End Sub
    Private Sub save1()
        Dim x As String = ""
        Dim Y As String = ""
        Dim j As Integer = 1
        Dim i As Integer = 1
        For j = 1 To 12
            x = x & ";" & Mid(M_554.data(j), 1, 4) & "/" & Mid(M_554.data(j), 5, 2) & "/" & Mid(M_554.data(j), 7, 2)
        Next
        For i = 1 To 12
            Y = Y & ";" & M_554.quant(i)
        Next
        '*********************** Save1 ****************************
        M_551.i = M_551.i + 1
        Select Case M_551.i
            Case Is = 10001
                Dim newline As IO.StreamWriter
                newline = New IO.StreamWriter(M_551.X, True)
                newline.WriteLine(M_551.supplier & " DCI" & ";" & "" & ";" & "Client:" & ";" & M_551.cliente & ";" & "Supplier:" & ";" & M_551.fornecedor)
                newline.WriteLine("" & ";" & "" & ";" & "Old Transmission:" & ";" & M_551.nr_transmit_antigo & ";" & "New Transmission:" & ";" & M_551.nr_transmit_novo & ";" & "Requirement Indicator")
                newline.WriteLine("" & ";" & "" & ";" & "Nr" & ";" & M_551.nr_rel & ";" & "Date:" & ";" & Mid(M_551.data_transmit, 1, 4) & "/" & Mid(M_551.data_transmit, 5, 2) & "/" & Mid(M_551.data_transmit, 7, 2) & ";" & M_551.indicador_req)
                newline.WriteLine()
                newline.WriteLine("PN" & x)
                newline.WriteLine(M_551.PN & Y)
                newline.Close()
            Case Is > 10001
                Dim newline As IO.StreamWriter
                newline = New IO.StreamWriter(M_551.X, True)
                newline.WriteLine(M_551.PN & Y)
                newline.Close()
        End Select
        '*************************** End ********************************
    End Sub

End Module
